﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetrieveCustomerFeedbackDetailByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerFeedbackDetailByCifRequest, RetrieveCustomerFeedbackDetailByCifResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerFeedbackDetailByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerFeedbackDetailByCifRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerFeedbackDetailByCifResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<CustomerFeedbackDetailRecord> records = new List<CustomerFeedbackDetailRecord>();
            List<CustomerFeedbackDetail> listDetail = Providers.Customers.RetrieveCustomerFeedbackDetailByCif(out totalRecord, Cif, pageSize, pageIndex);
            if (listDetail != null && listDetail.Count > 0)
            {
                records = listDetail.Select(x => new CustomerFeedbackDetailRecord
                {
                    Title = x.Title,
                    Creator = x.CreatorName,
                    Assigned = x.AssignedToName,
                    Modified = x.LastModifiedByName,
                    Created = x.Createdtime.ToShortDateString(),
                    ModifiedDate = x.Modifiedtime.ToShortDateString(),
                    Description = x.Description,
                    Solution = x.Solution,
                    RootCause = x.RootCause,
                    ServiceCategory = x.ServiceCategory,
                    Status1 = x.Status1,
                    Email1 = x.Email1
                }).ToList();
            }
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }


    }
}
